package com.cn.hjsj.appInterface.appImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;


import com.cn.hjsj.appInterface.appService.AppImei_bundleService;
import com.cn.hjsj.pojo.Imei_bundle;
import com.cn.hjsj.service.Imei_bundleService;
import com.cn.hjsj.util.http.HttpUtil;
import com.cn.hjsj.util.log.LogUtil;
import com.cn.hjsj.util.time.TimeUtil;

@Component("appImei_bundleImpl")
public class AppImei_bundleImpl implements AppImei_bundleService{
	
	@Resource(name = "imei_bundleService")
	private Imei_bundleService imei_bundleService;
	/*
	 * (non-Javadoc)
	 * 串号绑定
	 * @see com.cn.hjsj.appInterface.appService.AppImei_bundleService#SerialBinding(java.util.Map)
	 * update by lutao  2017-02-15
	 * problem： 缺少mock用户接口信息
	 */

	@Override
	public Map<String, Object> SerialBinding(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap=new HashMap<String,Object>() , map=new HashMap<String,Object>()
				, mapOne=new HashMap<String,Object>(), maps=new HashMap<String,Object>();
		try {
//<<<<<<< HEAD

			//需要获取用户信息 ---玛德魔筷的用户模块的获取用户信息接口还不知道   
//
//=======
//			//玛德魔筷的用户模块的获取用户信息接口还不知道    ---需要获取用户信息
//>>>>>>> refs/remotes/origin/master
			Integer userId=2;
			String userTel="123";
			maps.put("userId", userId);
			maps.put("productImei", object.getString("productImei"));
			//查询串号是否存在
			Imei_bundle imei_bundle = imei_bundleService.selectByUidAndImei(maps);
			if (imei_bundle!= null){
				if("0".equals(imei_bundle.getState().toString())){
					maps.put("id", imei_bundle.getId());
					maps.put("state", "1");
					maps.put("updateTime", TimeUtil.getNowStr());
					int flag1 = imei_bundleService.updateByPrimaryKey(maps);
					if(flag1 != 1){
		            	LogUtil.error("/AppImei_bundleImpl/SerialBinding:参数更新失败！");
		            	map.put("code", "40001");
		                map.put("mes", "将数据更新到数据库中失败");
		                return map;
		            }
				}
				LogUtil.info("/repair_imei_bundleControl/SerialBinding:调用成功");
				map.put("code", "10000");
				map.put("mes", "调用成功,该手机串号已绑定");				
			}else{
				//获取登录者的手机号连同串号发到erp接口中；				
				paramMap.put("userTel", userTel);
				paramMap.put("series", maps.get("productImei"));
				//不要怪我，要怪就怪还没有提供给我erp接口只能写个测试的url				
				String urls = "192.168.8.231:8084/erpQuery/orderControl/getInfo";
				
	            JSONObject objectT = JSONObject.fromObject(paramMap);
	            StringBuilder result = HttpUtil.sendJsonPost(urls, objectT.toString());
	            JSONObject object1 = JSONObject.fromObject(result.toString());
	            if(object1.get("result")=="fail"){
	            	LogUtil.error("/AppImei_bundleImpl/SerialBinding:解析参数异常！");
	                map.put("code", "40000");
	                map.put("mes", object1.get("message"));
	                return map;
	            }
	          //将数据绑定到数据库中
	            mapOne.put("productName", object1.getJSONObject("data").getString("productname"));
	            mapOne.put("productImei", maps.get("productImei").toString());
	            mapOne.put("userTel", userTel);
	            mapOne.put("userId", userId);
	            mapOne.put("newTime", object1.getJSONObject("data").getString("creation_date"));
	            mapOne.put("amount", object1.getJSONObject("data").getDouble("factretailmoney"));
	            mapOne.put("CreateTime", TimeUtil.getNowStr());
	            mapOne.put("updateTime", TimeUtil.getNowStr());
	            //将信息插入表中
	            
	            int flag = imei_bundleService.updateByPrimaryKey(mapOne);
	            if(flag != 1){
	            	LogUtil.error("/AppImei_bundleImpl/SerialBinding:参数插入失败！");
	            	map.put("code", "40001");
	                map.put("mes", "将数据绑定到数据库中失败");
	                return map;
	            }
	            LogUtil.info("/AppImei_bundleImpl/SerialBinding:调用成功");
				map.put("code", "10000");
				map.put("mes", "调用成功,该手机串号绑定成功");				
			}
			return map;		
		}catch (Exception e) {
			// TODO: handle exception
			LogUtil.error("/AppImei_bundleImpl/SerialBinding:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
			return map;
		}
	}
	/*
	 * (non-Javadoc)
	 * 串号解除绑定
	 * @see com.cn.hjsj.appInterface.appService.AppImei_bundleService#UnBinding(java.util.Map)
	 * update by lutao  2017-02-23
	 */
	@Override
	public Map<String, Object> UnBinding(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object>  map=new HashMap<String,Object>(),maps=new HashMap<String,Object>();
		try{
			//获取id通过id来更新imei的状态
			maps.put("id", object.getInt("id"));
			maps.put("state", 0);
			maps.put("updateTime", TimeUtil.getNowStr());
			System.out.println("---maps:"+maps.toString());
			int flag = imei_bundleService.updateByPrimaryKey(maps);
			if(flag!=1){
				LogUtil.error("/AppImei_bundleImpl/UnBinding:参数更新失败！");
            	map.put("code", "40001");
                map.put("mes", "将数据更新到数据库中失败");
                return map;
			}
			LogUtil.info("/AppImei_bundleImpl/UnBinding:调用成功");
			map.put("code", "10000");
			map.put("mes", "调用成功,该手机串号解绑成功");
			return map;
		}catch(Exception e){
			LogUtil.error("/AppImei_bundleImpl/UnBinding:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
			return map;
		}
		
	}
	
	@Override
	public Map<String, Object> InsuredList(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object>  map=new HashMap<String,Object>()
				,mapOne=new HashMap<String,Object>()
				,mapTwo=new HashMap<String,Object>()
				,maps=new HashMap<String,Object>();
		JSONArray imei_bundle = new JSONArray();
		try{
			//获取用户信息：userId 需要魔块的用户接口		
			Integer userId = 2;		
			
			maps.put("userId", userId);
			System.out.println("---maps:"+maps.toString());
			//获取 imei 列表
			List<Imei_bundle> imeiList = imei_bundleService.getList(maps);
			int count = imei_bundleService.getListCount(maps);
			if(imeiList.size()!=0){
				for (int i=0;i<imeiList.size();i++){
					mapOne.put("productName", imeiList.get(i).getProductname());
					mapOne.put("productImei", imeiList.get(i).getProductimei());
					mapOne.put("newTime", imeiList.get(i).getNewtime());
					imei_bundle.add(mapOne);
				}
				
				LogUtil.info("/AppImei_bundleImpl/InsuredList:调用成功");
				mapTwo.put("imei", imei_bundle);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功");
				
			}else{
				LogUtil.info("/AppImei_bundleImpl/InsuredList:调用成功");
				mapTwo.put("imei", imei_bundle);
				mapTwo.put("count", count);				
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功,数据库中无数据");
				
			}
			return map;
		}catch(Exception e){
			LogUtil.error("/AppImei_bundleImpl/InsuredList:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
			return map;
		}
	}
	/*
	 * 过保
	 * (non-Javadoc)
	 * @see com.cn.hjsj.appInterface.appService.AppImei_bundleService#Guarantee(net.sf.json.JSONObject)
	 */
//<<<<<<< HEAD

//	@Override
//	public Map<String, Object> Guarantee(JSONObject object) {
//		// TODO Auto-generated method stub
//		Map<String, Object>  map=new HashMap<String,Object>();
//		try{
//			int days = TimeUtil.daysBetween(object.getString("newTime"), TimeUtil.getNowStr());
//			if(days>356){
//				
//			}
//			return null;
//		}catch(Exception e){
//			LogUtil.error("/AppImei_bundleImpl/Guarantee:解析参数异常！"+e.getMessage());
//			map.put("code", "30000");
//			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
//			return map;	
//		}
//				
//	}
//=======
//	@Override
//	public Map<String, Object> Guarantee(JSONObject object) {
//		// TODO Auto-generated method stub
//		Map<String, Object>  map=new HashMap<String,Object>();
//		try{
//			int days = TimeUtil.daysBetween(object.getString("newTime"), TimeUtil.getNowStr());
//			if(days>356){
//				
//			}
//			return null;
//		}catch(Exception e){
//			LogUtil.error("/AppImei_bundleImpl/Guarantee:解析参数异常！"+e.getMessage());
//			map.put("code", "30000");
//			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
//			return map;	
//		}
//				
//	}
//>>>>>>> refs/remotes/origin/master

}
